package org.fireflow2.designer.eclipse.actions;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.gef.EditPart;
import org.eclipse.gef.RequestConstants;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.requests.CreateRequest;
import org.eclipse.gef.ui.actions.SelectionAction;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.IWorkbenchPart;
import org.fireflow2.designer.eclipse.Activator;
import org.fireflow2.designer.eclipse.parts.designer.ActivityPart4Designer;
import org.fireflow2.designer.eclipse.parts.tree.ActivityTreePart;

public class AddToolTaskAction extends SelectionAction {
	public static String ID = "CREATE_TOOL_TASK";
	public AddToolTaskAction(IWorkbenchPart part) {
		super(part);
		setLazyEnablementCalculation(false);
	}
	@Override
	protected boolean calculateEnabled() {
		Command cmd = createNewTaskCommand(getSelectedObjects());
		if (cmd == null)
			return false;
		return cmd.canExecute();
	}
	public Command createNewTaskCommand(List objects) {
		if (objects.isEmpty())
			return null;
		if (!(objects.get(0) instanceof EditPart))
			return null;
		EditPart editPart = (EditPart)objects.get(0);
		if (!(editPart instanceof ActivityPart4Designer)
				&& !(editPart instanceof ActivityTreePart)
//				&& !(editPart instanceof TasksTreePart)
				){
			return null;
		}
		Map createTaskProperties = new HashMap();
		createTaskProperties.put("ChildType", "Task.TOOL");
		createTaskProperties.put("Parent", editPart.getModel());
		CreateRequest createReq =
			new CreateRequest(RequestConstants.REQ_CREATE);
		createReq.setExtendedData(createTaskProperties);


		Command cmd = editPart.getCommand(createReq);
		return cmd;

	}
	protected void init() {
		super.init();
		setText("Add Tool Task");
		setToolTipText("Add Tool Task");
		setId(ID);
		
		ImageDescriptor smallIcon = ImageDescriptor.createFromFile(Activator.class, "resources/tool_task_16.gif");
//		ISharedImages sharedImages = PlatformUI.getWorkbench().getSharedImages();
		setImageDescriptor(smallIcon);
//		setDisabledImageDescriptor(sharedImages.getImageDescriptor(
//				ISharedImages.IMG_TOOL_DELETE_DISABLED));
		setEnabled(false);
	}

	/**
	 * Performs the delete action on the selected objects.
	 */
	public void run() {
		execute(createNewTaskCommand(getSelectedObjects()));
	}

}
